<?php

$auth = Auth::getAuth('current_user');
$db = new Database;
$fs = new functions;

$date = $fs->currentDateTime();
$id = $_POST['report_id'];

$companyDoc = new Company($db, $auth['company_id']);
$personDoc = new Person($db, $auth['id']);

if ($id == 0) {
    //new doc
    $reportDoc = new Report($db);
    $reportDoc->form_id = $_POST['form_id'];
    $reportDoc->title = $_POST['title'];
    $reportDoc->description = $_POST['description'];
    $reportDoc->parameters = $_POST['parameters'];
    $reportDoc->columns = $_POST['columns'];
    $reportDoc->plotbands = $_POST['plotBands'];
    $reportDoc->symbol = $_POST['symbol'];
    $reportDoc->yaxis_max = $_POST["yAxisMax"];
    $reportDoc->yaxis_min = $_POST["yAxisMin"];
    $reportDoc->yaxis_interval = $_POST["yAxisInterval"];
    $reportDoc->company = $companyDoc;
    $reportDoc->date_created = $date;
    $reportDoc->created_by = $personDoc;
    $reportDoc->is_active = '1';
    $reportDoc->save();
} else {
    //edit doc
    $reportDoc = new Report($db, $id);
    $reportDoc->form_id = $_POST['form_id'];
    $reportDoc->title = $_POST['title'];
    $reportDoc->description = $_POST['description'];
    $reportDoc->parameters = $_POST['parameters'];
    $reportDoc->columns = $_POST['columns'];
    $reportDoc->plotbands = $_POST['plotBands'];
    $reportDoc->symbol = $_POST['symbol'];
    $reportDoc->yaxis_max = $_POST["yAxisMax"];
    $reportDoc->yaxis_min = $_POST["yAxisMin"];
    $reportDoc->yaxis_interval = $_POST["yAxisInterval"];
    $reportDoc->company = $companyDoc;
    $reportDoc->date_updated = $date;
    $reportDoc->updated_by = $personDoc;
    $reportDoc->is_active = '1';
    $reportDoc->update();
}

echo 'Report(' . $reportDoc->title . ') has been successfully saved.';
?>
